package com.winchain.chainbill.bill.biz.discount.rx;

import com.shell.framework.command.ChainBase;
import com.shell.framework.command.CommandContext;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.basic.charge.cmd.ValRetBizCmd;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillTranEvent;
import com.winchain.chainbill.bill.biz.contract.cmd.ContractCancelCmd;
import com.winchain.chainbill.bill.biz.discount.cmd.BillDctCmd;
import com.winchain.chainbill.bill.biz.discount.cmd.BillDctFinQuotaCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillInvoiceRejectCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillMoveCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillOptCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillTimeoutCmd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillDctRevRejectPrntSubscriber extends OnErrorNotImplementedSubscriber<BillTranEvent<CbDct>> {
	@Autowired
	private BillTimeoutCmd<CbDct> billTimeoutCmd;
	@Autowired
	private BillOptCmd<CbDct> billOptCmd;
	@Autowired
	private BillMoveCmd<CbDct> billMoveCmd;
	@Autowired
	private BillDctCmd billDctCmd;
	@Autowired
	private ContractCancelCmd<CbDct> contractCancelCmd;
	@Autowired
	private ValRetBizCmd valRetBizCmd;
	@Autowired
	private BillInvoiceRejectCmd billInvoiceRejectCmd;
	@Autowired
	private BillDctFinQuotaCmd billDctFinQuotaCmd;

	public void onApplicationEvent(BillTranEvent<CbDct> t) {
		Bill<CbDct> source = t.getBill();

		CbOpt opt = new CbOpt();
		opt.setCurState("S013");
		opt.setOptCode("E003");
		opt.setRemark("��������������");

		t.setOperation(opt);

		CommandContext<Bill<CbDct>> context = new CommandContext();
		context.setSource(source);
		context.setValue("Operation", t.getOperation());
		context.setValue("TimeoutOperation", t.getTimeoutOpt());
		context.setValue("valRetBizState", "D");
		context.setValue("quotaOptType", "qt_ubk");

		ChainBase<Bill<CbDct>> crb = new ChainBase();
		crb.add(this.billTimeoutCmd);
		crb.add(this.billOptCmd);
		crb.add(this.billMoveCmd);
		if (((CbDct) source.getSource()).getDctId() != null) {
			((CbDct) source.getSource()).setDctState("0");
			((CbDct) source.getSource()).setPmtState("1");
			((CbDct) source.getSource()).setDctQtOpt("1");

			crb.add(this.billDctFinQuotaCmd);
			crb.add(this.billDctCmd);
			crb.add(this.valRetBizCmd);
			crb.add(this.contractCancelCmd);
			context.setValue("bizId", ((CbDct) source.getSource()).getDctId());
			context.setValue("invType", "4");
			crb.add(this.billInvoiceRejectCmd);
		}
		crb.execute(context);
	}
}
